package io.renren.modules.udp.utils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import io.renren.common.utils.PublicUtil;
import io.renren.common.utils.RedisUtils;
import io.renren.modules.udp.entity.R5rmObjectsEntity;
import io.renren.modules.udp.entity.R5rmSkillstatusEntity;
import io.renren.modules.udp.service.R5rmObjectsService;
import io.renren.modules.udp.service.R5rmSkillstatusService;
import io.renren.modules.udp.service.SpringBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class UdpClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
		// System.out.println("1:"+msg);
		// System.out.println("2:"+msg.content());
		System.out.println("UTF_8:" + msg.content().toString(CharsetUtil.UTF_8));

		byte[] bytes = new byte[msg.content().readableBytes()];

		msg.content().readBytes(bytes);
		String s1= UdpStringUtils.bytes2Str(bytes);

		if(!s1.isEmpty()){
			System.out.println("str:"+s1);
			if(s1.startsWith(UdpConstant.MSG_HEADER_GETFILELENGTH)){
				String slengh = s1.substring(s1.length()-11);
				System.out.println("lenth:"+slengh);
			}
			//获取监控参数
			if (s1.startsWith(UdpConstant.MSG_HEADER_ACCESSEQUIP_S)) {
				R5rmSkillstatusService r5rmSkillstatusService = (R5rmSkillstatusService) SpringBeanUtils.getBean("r5rmSkillstatusService");
				R5rmSkillstatusEntity skillstatusEntity = r5rmSkillstatusService.getUdp(s1);
				r5rmSkillstatusService.insert(skillstatusEntity);
			}
			if(s1.startsWith(UdpConstant.MSG_HEADER_GETFILE)){
				String src = "30 35 35 31 38 31 31 32 39 30 31 20 30 30 30 30 30 30 30 30 20 66 6A 6C 6A 23 31 2D 3E BF A8 BA C5 38 39 38 36 30 34 30 32 31 30 31 37 30 31 36 35 34 33 33 39 0D 0A 2D 2D 2D 5B 32 35 35 5D 2D 5B 30 31 5D 66 6A 6C 6A 23 31 0D 0A 30 35 35 31 38 31 31 32 39 30 32 20 30 30 30 30 30 30 30 30 20 66 6A 6C 6A 23 32 2D 3E BF A8 BA C5 38 39 38 36 30 34 30 32 31 30 31 37 30 31 36 35 34 33 34 30 0D 0A 2D 2D 2D 5B 32 35 35 5D 2D 5B 30 31 5D 66 6A 6C 6A 23 32 0D 0A 30 35 35 31 38 31 31 32 39 30 33 20 30 30 30 30 30 30 30 30 20 66 6A 6C 6A 23 33 2D 3E BF A8 BA C5 38 39 38 36 30 34 30 32 31 30 31 37 30 31 36 35 34 33 34 31 0D 0A 2D 2D 2D 5B 32 35 35 5D 2D 5B 30 31 5D 66 6A 6C 6A 23 33 0D 0A 30 35 35 31 38 31 31 32 39 30 34 20 30 30 30 30 30 30 30 30 20 66 6A 6C 6A 23 34 2D 3E BF A8 BA C5 38 39 38 36 30 34 30 32 31 30 31 37 30 31 36 35 34 33 34 32 0D 0A 2D 2D 2D 5B 32 35 35 5D 2D 5B 30 31 5D 66 6A 6C 6A 23 34 0D 0A";
				System.out.println("file:"+UdpStringUtils.hexStrToStr(src));
			}
			//获得设备信息
			if (s1.startsWith(UdpConstant.MSG_HEADER_GETEQUIPSTATUS)) {
				// 实例化service
				R5rmObjectsService r5rmObjectsService = (R5rmObjectsService) SpringBeanUtils.getBean("r5rmObjectsService");
				R5rmObjectsEntity r5rmObjectsEntity = new R5rmObjectsEntity();
				String[] s2 = s1.split("\\s+");
				//设备号
				String zh = s2[3] + s2[4] + s2[5] + s2[6] + s2[7] + s2[8] + s2[9] + s2[10] + s2[11] + s2[12] + s2[13];
				//设备状态
				String status = UdpStringUtils.hexStrToStr(s2[14]);
				r5rmObjectsEntity.setObjCode(UdpStringUtils.hexStrToStr(zh));
				//查询出该设备号的设备信息
				R5rmObjectsEntity entityList = r5rmObjectsService.selectOne(
						new EntityWrapper<R5rmObjectsEntity>().eq("obj_code",r5rmObjectsEntity.getObjCode()));
				//将id set 进去
				r5rmObjectsEntity.setObjId(entityList.getObjId());
				switch (status) {
					case "0":
						r5rmObjectsEntity.setObjUdfchar05("不在线");
						break;
					case "1":
						r5rmObjectsEntity.setObjUdfchar05("在线");
						break;
					case "2":
						r5rmObjectsEntity.setObjUdfchar05("不存在此设备");
						break;
				}
				r5rmObjectsEntity.setObjUdfchar04(status);
				r5rmObjectsService.updateById(r5rmObjectsEntity);
			}
		}else{
			System.out.println("response is null");
		}

	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}

	public static void main(String[] args) {
		String s1= "FD DF 50 30 35 35 31 38 31 31 32 39 30 32 00 64 FF AA 55 5F FF 02 11 00 08 56 07 00 00 09 69 00 00 05 72 00 00 09 7C 00 F2 00 00 00 00 00 00 00 01 38 00 07 00 00 00 03 0B 00 46 00 02 67 00 00 A9 06 2F 00 00 ED 00 07 E1 00 08 B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 22 00 00 00 00 12 11 00 00 00 00 00 00 00 08 F3 00";
		String[] s2 = s1.split("\\s+");
		String wz = s2[24];
		System.out.println(wz);
		R5rmSkillstatusEntity skillstatusEntity = new R5rmSkillstatusEntity();
		//根据文档算好数组起始
		int s = 19;
		//设备号码
		String zh = s2[3] + s2[4] + s2[5] + s2[6] + s2[7] + s2[8] + s2[9] + s2[10] + s2[11] + s2[12] + s2[13];
		skillstatusEntity.setSsObj(UdpStringUtils.hexStrToStr(zh));
		//光伏板输入电压(V)
		skillstatusEntity.setSsUdfchar01(PublicUtil.result(s2[s+10]+s2[s+11],2,"V"));
		//光伏板输入电流(A)
		skillstatusEntity.setSsUdfchar02(PublicUtil.result(s2[s+12]+s2[s+13],1,"A"));
		//光伏板输入功率(W)
		skillstatusEntity.setSsUdfchar03(PublicUtil.result(s2[s+59]+s2[s+60],0,"W"));
		//光感输入电压(V)    有误
		skillstatusEntity.setSsUdfchar04(PublicUtil.result(s2[s+44]+s2[s+45],2,"V"));
		//LED驱动器输出电压（V)
		skillstatusEntity.setSsUdfchar05(PublicUtil.result(s2[s+20]+s2[s+21],1,"V"));
		//LED驱动器输出电流（A)
		skillstatusEntity.setSsUdfchar06(PublicUtil.result(s2[s+22]+s2[s+23],2,"A"));
		//LED驱动器输出功率（W)
		skillstatusEntity.setSsUdfchar07(PublicUtil.result(s2[s+24]+s2[s+25],1,"W"));
		//LED驱动器输出状态  有误
		skillstatusEntity.setSsUdfchar08("有误");
		//LED驱动器已输出时间（H) ====LED输出总时间
		skillstatusEntity.setSsUdfchar09(Long.parseLong(s2[s+37], 16)+"H");
		//蓄电池电压（V)
		skillstatusEntity.setSsUdfchar10(PublicUtil.result(s2[s+18]+s2[s+19],2,"V"));
		//蓄电池充电电流（A)
		skillstatusEntity.setSsUdfchar11(PublicUtil.result(s2[s+61]+s2[s+62],1,"A"));
		//蓄电池充电功率（W)
		skillstatusEntity.setSsUdfchar12(PublicUtil.result(s2[s+63]+s2[s+64],0,"W"));
		//蓄电池总充电量（度）
		skillstatusEntity.setSsUdfchar13(PublicUtil.result(s2[s+54]+s2[s+55]+s2[s+56],1,"度"));
		//蓄电池剩余电量比
		skillstatusEntity.setSsUdfchar14(Long.parseLong(s2[s+30], 16)+"%");
		//蓄电池当前状态
		long dczt = Long.parseLong(s2[s+31], 16);
		if (dczt==0){
			skillstatusEntity.setSsUdfchar15("正常");
		}else if (dczt==1){
			skillstatusEntity.setSsUdfchar15("过压");
		}else if (dczt==2){
			skillstatusEntity.setSsUdfchar15("欠压");
		}else {
			skillstatusEntity.setSsUdfchar15("获取出错");
		}
		//风机输入直流电压(V)
		skillstatusEntity.setSsUdfchar16(PublicUtil.result(s2[s+14]+s2[s+15],2,"V"));
		//风机输入直流电流(A)
		skillstatusEntity.setSsUdfchar17(PublicUtil.result(s2[s+16]+s2[s+17],1,"A"));
		//风机热效电流(W)  有误
		skillstatusEntity.setSsUdfchar18(PublicUtil.result(s2[s+8]+s2[s+9],2,"A"));
		//风机输入直流功率电压(V)  有误
		skillstatusEntity.setSsUdfchar19(PublicUtil.result(s2[s+10]+s2[s+11],2,"V"));
		//风机有限充电电流（A)
		skillstatusEntity.setSsUdfchar20(PublicUtil.result(s2[s+83]+s2[s+84],1,"A"));
		//风机卸荷平均电流（A)
		skillstatusEntity.setSsUdfchar21(PublicUtil.result(s2[s+81]+s2[s+82],1,"A"));
		//风机当前转速（Rpm）
		skillstatusEntity.setSsUdfchar22(PublicUtil.result(s2[s+79]+s2[s+80],0,"Rpm"));
		//风机运行状态 有误
		skillstatusEntity.setSsUdfchar23("有误");
		//第一路输出电流（A)
		skillstatusEntity.setSsUdfchar24(PublicUtil.result(s2[s+69]+s2[s+70],1,"A"));
		//第一路输出功率（W)
		skillstatusEntity.setSsUdfchar25(PublicUtil.result(s2[s+73]+s2[s+74],0,"W"));
		//第一路输出状态
		skillstatusEntity.setSsUdfchar26("有误");
		//第一路控制模式
		skillstatusEntity.setSsUdfchar27("有误");
		//控制箱温度
		skillstatusEntity.setSsUdfchar29(PublicUtil.result(s2[s+10]+s2[s+11],2,"A"));
		//摄像头当前状态
		skillstatusEntity.setSsUdfchar32("有误");
		//摄像头连接状态
		skillstatusEntity.setSsUdfchar33("有误");
		//摄像头输出电压（V)
		skillstatusEntity.setSsUdfchar35(PublicUtil.result(s2[s+10]+s2[s+11],2,"V"));
	}

	/**
	 * 16进制转10进制获取数值
	 * @param s1
	 * @param s2
	 * @param s3
	 * @return
	 */
	/*public static String result(String s1,int s2,String s3){
		String s4 ;
		if (s2 != 0) {
			s4 = new BigDecimal((float)Long.parseLong(s1, 16)/100).setScale(s2, BigDecimal.ROUND_HALF_UP).doubleValue() + s3;
		} else {
			s4 = new BigDecimal((float)Long.parseLong(s1, 16)/100).setScale(s2, BigDecimal.ROUND_HALF_UP).intValue() + s3;
		}
		return s4;
	}*/

}
